<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <link
            rel="stylesheet"
            href="https://static.web.sdo.com/chuanshi/common/style/bootstrap.min.css"
        />
        <script src="https://cdn.bootcdn.net/ajax/libs/axios/1.7.3/axios.min.js"></script>
        <script src="/js/request.js"></script>
        <title>用户界面</title>
        <style>
            .wrapper {
                position: relative;
                width: 100vw;
                height: 100vh;
                display: flex;
                flex-direction: column;
                justify-content: center;
                align-items: center;
                background-color: #ddd;
            }
            .box {
                width: 360px;
                padding: 30px 30px 40px 30px;
                background-color: #fff;
                box-shadow: 1px 1px 12px rgba(0, 0, 0, 0.6);
            }
            img {
                display: block;
                width: 50px;
                margin: 0px auto 20px auto;
            }
            .box {
                width: 400px;
            }
            .list-group-item {
                display: flex;
                align-items: center;
                justify-content: space-between;
            }
        </style>
    </head>
    <body>
        <div class="wrapper">
            <div class="box">
                <div class="user-add">
                    <h4>
                        欢迎您,<strong>用户名</strong>
                        <div class="btn btn-danger btn-logout">注销</div>
                    </h4>
                    <div class="form-group">
                        <input
                            type="text"
                            class="form-control username"
                            placeholder="用户名"
                        />
                    </div>
                    <div class="form-group">
                        <input
                            type="text"
                            class="form-control password"
                            placeholder="密码"
                        />
                    </div>
                    <button class="btn btn-block btn-primary btn-add">
                        添加用户
                    </button>
                </div>
                <hr />
                <ul class="list-group"></ul>
            </div>
        </div>
    </body>
</html>
<script>
    const btnAdd = document.querySelector(".btn-add");
    const btnLogout = document.querySelector(".btn-logout");
    const username = document.querySelector(".username");
    const password = document.querySelector(".password");
    btnAdd.addEventListener("click", async () => {
        if (username.value && password.value) {
            const res = await request({
                method: "post",
                url: "/user",
                data: {
                    username: username.value,
                    password: password.value,
                },
            });
            username.value = "";
            password.value = "";
            getList();
        }
    });
    btnLogout.addEventListener("click", () => {
        axios.get("/api/account/logout").then((res) => {
            location.href = "/login";
        });
    });
    const getList = async () => {
        const res = await request({
            url: "/user",
        });
        if (res.code !== 200) {
            return (window.location.href = "/login");
        }
        var html = "";
        res.data.forEach((item) => {
            html += `<li class="list-group-item">${item.username}<button class="btn btn-danger btn-delete">删除</button></li>`;
        });
        document.querySelector(".list-group").innerHTML = html;
    };
    getList();
</script>
